const path = require('path');
const HtmlPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
//thread-loader
//happypack
//tree-shaking

module.exports={
	mode:'development',
	entry:{
		home:'./src/pages/home/index.js',
		about:'./src/pages/about/index.js'
	},
	output:{
		// publicPath:'/webpack-multi-page-demo/dist/',
		path:path.resolve(__dirname,'dist'),
		filename:'pages/[id]/index.js'
	},
	module:{
		noParse:/node_modules\/(jquery\.js)/,
		rules:[
			{
				test:/\.js$/,
				loader:"babel-loader",
				include:[
					path.resolve(__dirname, "src")
				],
				exclude:[
					path.resolve(__dirname, "node_modules")
				],
				options:{
					presets:["@babel/preset-env"]
				}
			},
			{
				test:/\.html$/,
				loader:"html-loader"
			},
			{
				test:/\.css$/,
				use:[
					"style-loader",
					"css-loader",
					{
						loader: "postcss-loader",
			            options: {
			              postcssOptions: {
			                plugins: [
			                  [
			                    "postcss-preset-env",
			                    {
			                      // Options
			                    },
			                  ],
			                ],
			              },
			            },
			        },
			        {
			        	loader: 'px2rem-loader',
				        // options here
				        options: {
				          remUni: 100,
				          remPrecision: 5
				        }
				    }
				]
			},
			{
				test:/\.(jpg|png|jpeg|gif)$/i,
				use:[
					{
						loader:"url-loader",
						options:{
							name:'assets/img/[name]-[hash].[ext]',
							limit:5000//5kb
						}
					}
				]
			},
			{
	            test: require.resolve("jquery"),
		        loader: "expose-loader",
		        options: {
		          exposes: ["$", "jQuery"],
		        }
	        }
		]
	},
	plugins:[

		new HtmlPlugin({
			template:'src/pages/home/index.html',
			filename:'pages/home/index.html',
			title:'home',
			inject:'head',
			chunks:['home']	
		}),
		new HtmlPlugin({
			template:'src/pages/about/index.html',
			filename:'pages/about/index.html',
			title:'about',
			inject:'head',
			chunks:['about']
			
		}),
		new CleanWebpackPlugin(),
		// new webpack.NamedModulesPlugin(),
		new webpack.HotModuleReplacementPlugin()
		// new BundleAnalyzerPlugin()
	],
	//压缩相关
	optimization: {
		minimize: true,
	    minimizer: [
	    	new TerserPlugin({
			parallel: true,
			terserOptions: {
	          compress: {
	          	unused:true,
	          	drop_debugger:true,
	          	drop_console:true,
	          	dead_code:true
	          }
	        }
		})],
	},
	devtool:'inline-source-map',
	devServer:{
		contentBase:'./dist',
		hot:true
	}
}